package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.DifferScanTable;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/24 0024
 *
 * 差异扫描
 */

public class DifferScanTableDao extends BaseDao<DifferScanTable, Long> {

  public DifferScanTableDao() {
    super(DbHelper.getInstance(BaseApplication.getContext()), DifferScanTable.class);
  }

  /**
   * 查询对应子单是否扫描了
   */
  public int findChildWayBillNoStatus(String differCode, String waybillNo, String waybillChildNo) {
    int status = -1;
    try {
      QueryBuilder<DifferScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("diffreportNo", differCode).and().eq("waybillNo", waybillNo).and().eq("waybillChildNo", waybillChildNo);

      List<DifferScanTable> differScanTableList = queryBuilder.query();
      if (differScanTableList != null && differScanTableList.size() > 0) {
        status = differScanTableList.get(0).getScanStatus();
      }
      return status;
    } catch (SQLException e) {
      e.printStackTrace();
      status = -1;
    }
    return status;
  }

  /**
   * 根据差异编码和子单号查询数据
   */
  public List<DifferScanTable> getDataByDifferCodeAndWaybillNo(String differCode, String waybillNo) {
    try {
      QueryBuilder<DifferScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("diffreportNo", differCode).and().eq("waybillNo", waybillNo);
      List<DifferScanTable> list = queryBuilder.query();
      return list;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 查询未上传的数据
   */
  public List<DifferScanTable> getUnUploadDataList() {
    try {
      QueryBuilder<DifferScanTable, Long> queryBuilder = dao.queryBuilder();
      List<DifferScanTable> differScanTableList = queryBuilder.where().eq("isUpload", 0).query();
      return differScanTableList;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 更新表数据对象
   */
  public int updataItem(DifferScanTable itemTable) {
    int result = -1;
    try {
      result = this.update(itemTable);
    } catch (SQLException e) {
      e.printStackTrace();
      result = -1;
    }
    return result;
  }

  /**
   * 查询未上传的数量
   */
  public int queryNotUploadDataNum() {
    int totalNum = 0;
    List<DifferScanTable> mTableList;
    try {
      mTableList = super.findAll();
      if (mTableList != null && mTableList.size() > 0) {
        for (DifferScanTable itemTable : mTableList) {
          int isUpload = itemTable.getIsUpload();
          if (isUpload == 0) {
            totalNum++;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return totalNum;
  }
}
